package com.woniu.bh.beautysalon.web.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.bh.beautysalon.model.dto.CouponsDto;
import com.woniu.bh.beautysalon.model.params.CouponsParam;
import com.woniu.bh.beautysalon.web.common.DateUtils;
import com.woniu.bh.beautysalon.web.dao.BeautysalonCouponMapper;
import com.woniu.bh.beautysalon.web.dao.CustomerCouponMapper;
import com.woniu.bh.beautysalon.web.service.IBeautysalonCouponService;
import com.woniu.bh.common.core.domain.coupon.BeautysalonCoupon;
import com.woniu.bh.common.core.domain.customer.CustomerCoupon;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author daniel
 * @since 2020-10-16
 */
@Service
public class BeautysalonCouponServiceImpl extends ServiceImpl<BeautysalonCouponMapper, BeautysalonCoupon> implements IBeautysalonCouponService {
    @Autowired
    private BeautysalonCouponMapper beautysalonCouponMapper;

    @Autowired
    private CustomerCouponMapper customerCouponMapper;

    /**
     * @description: 获取所有优惠券
     * @param: beautysalonId
     * @return: java.util.List<com.woniu.bh.beautysalon.model.dto.CouponsDto>
     */
    @Override
    public List<CouponsDto> getAllCoupons(Integer beautysalonId) {
        List<CouponsDto> coupons = beautysalonCouponMapper.getAllCoupons(beautysalonId);
        List<CouponsDto> couponsDtos = new ArrayList<>();
        CouponsDto couponsDto = new CouponsDto();
        for (CouponsDto c : coupons) {
            couponsDto.setCouponValidTime(c.getCouponValidTime());
            couponsDto.setCouponType(c.getCouponType());
            couponsDto.setCouponName(c.getCouponName());
            couponsDto.setCouponImg(c.getCouponImg());
            couponsDto.setCouponDesc(c.getCouponDesc());
            couponsDto.setBeautysalonCouponUsedCount(c.getBeautysalonCouponUsedCount());
            couponsDto.setBeautysalonCouponTakeCount(c.getBeautysalonCouponTakeCount());
            System.out.println(c.getCouponValidTime());
            couponsDto.setCouponValidTime(c.getCouponValidTime() / (1000 * 24 * 60 * 60));
            //时间转换 long->date
            Date date = new Date((Long.parseLong(c.getBeautysalonCouponStartTime())));
            String startTime = DateUtils.dateToDateString(date);
            couponsDto.setBeautysalonCouponStartTime(startTime);
            Date date1 = new Date(Long.parseLong(c.getBeautysalonCouponEndTime()));
            String endTime = DateUtils.dateToDateString(date1);
            couponsDto.setBeautysalonCouponEndTime(endTime);
            Date date2 = new Date(Long.parseLong(c.getBeautysalonCouponTime()));
            String time = DateUtils.dateToDateString(date2);
            couponsDto.setBeautysalonCouponTime(time);
            couponsDtos.add(couponsDto);
        }
        return couponsDtos;
    }

    /**
     * @description: 发放优惠券
     * @param: couponsParam
     * @return: int
     */
    @Override
    public int distributeCoupons(CouponsParam couponsParam) {
        //优惠券数量判断
        List<Integer> customerList = couponsParam.getCustomerList();
        BeautysalonCoupon beautysalonCoupon = beautysalonCouponMapper.selectById(couponsParam.getCouponId());
        if (beautysalonCoupon.getBeautysalonCouponTakeCount() < customerList.size()) {
            return -1;
        }
        CustomerCoupon customerCoupon = null;
        int count = 0;
        for (Integer customerId : customerList) {
            QueryWrapper<CustomerCoupon> customerCouponQueryWrapper = new QueryWrapper<>();
            customerCouponQueryWrapper.eq("customer_id",customerId);
            CustomerCoupon customerCoupon1 = customerCouponMapper.selectOne(customerCouponQueryWrapper);
            customerCoupon = new CustomerCoupon();

            customerCoupon.setCustomerId(customerId);
            customerCoupon.setCustomerCouponGetTime(System.currentTimeMillis());
            //用户是否已经拥有某种优惠券

            if (customerCoupon1.getCouponId().equals(couponsParam.getCouponId()) && customerCoupon1.getCouponId() != null) {
                //System.out.println(customerCoupon1.getCustomerCouponCount());
                customerCoupon.setCustomerCouponCount(customerCoupon1.getCustomerCouponCount() + 1);
                customerCoupon.setCustomerCouponId(customerCoupon1.getCustomerCouponId());
                customerCouponMapper.updateById(customerCoupon);
            } else {
                customerCoupon.setCouponId(couponsParam.getCouponId());
                customerCoupon.setCustomerCouponCount(1);
                customerCouponMapper.insert(customerCoupon);
            }
            count++;
        }

        return count;
    }
}
